home *** CD-ROM | disk | FTP | other *** search
- Path: cymbal.aix.calpoly.edu!not-for-mail
- From: dstubbs@cymbal.aix.calpoly.edu (Dan Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: Fastest way to computer log(base2) of x?
- Date: 25 Jan 1996 17:44:04 -0800
- Organization: California Polytechnic State University, San Luis Obispo
- Message-ID: <4e9bl4$3ccp@cymbal.aix.calpoly.edu>
- References: <4e61iu$p6e@villa.fc.net> <4e6p7t$1n72@cymbal.aix.calpoly.edu> <4e8r54$n8q@ns.RezoNet.NET>
- NNTP-Posting-User: dstubbs@cymbal.aix.calpoly.edu
-
- In article <4e8r54$n8q@ns.RezoNet.NET>, Ray Dunn <ray@ultimate-tech.com> wrote:
- >In referenced article, Dan Stubbs says...
-
- >>It seems like an interesting exercise to find the left bit using
- >>binary search since moving an appropriate mask around (for
- >>speed) is a bit "different." The following is for 32 bit ints,
- >>as you can see it is simple to modify for any width int that is
- >>a power of 2.
- >>
- >>/*------------------------------------------------------------------*/
- >>int left_most_bit (int k) {
- >>/*
- >> * returns the position of the left most bit in k assuming that
- >> * k != 0 and 32 bit ints. The algorithm used is essentially a
- >> * binary search.
- >> */
- >> unsigned posn = 0, width = 16, mask = 0xffff0000;
- >>
- >> while (width > 1) {
- >> if (k & mask) {
- >> width /= 2;
- >> mask <<= (posn + width);
- >> mask >>= posn;
- >> }
- >> else {
- >> mask >>= width;
- >> posn += width;
- >> }
- >> }
- >> if (k & mask) return posn;
- >> else return posn+1;
- >>}
- >
- >I'm sorry, but there are an unreasonable number of errors in this
- *not true* -->^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- >code. It's difficult to even see what you're trying to do, let alone
- *It does a binary search* -->^^^^^^^^^^^^^^^^^^^^^^^^^^
- >correct things like mask should be of unsigned type, and a possible
- >reliance on right shift inserting zeros. Please compile and test
- >before posting.
- >
-
- Indeed, the code above was compiled and tested. As advertised, it
- implements binary search in the "unusual" case where searching half
- the remaining list is accomplished using the & operator and an
- appropriate mask. It correctly identified the left most bit for
- over two billion test integers.
-
- The only "error" in the code above is that int should be changed to
- unsigned in the declaration--so that it will work correctly for *all*
- compilers. The code above has been so changed. I still think it is an
- interesting variation on implementing binary search.
-
- Binary search is probably relatively slow in this case because the over-
- head of binary search doesn't really pay off while searching only
- 32 bits. Even sequential search will probably be faster--and certainly
- much shorter. For example, here is a (compiled and tested) implementation
- of sequential search.
-
- +----------------------------------------------------+
- int left_most_bit (int k) {
- unsigned posn = 0, mask = 0x80000000;
-
- while (!(k & mask)) {
- mask >>= 1;
- posn++;
- }
- return posn;
- }
- +----------------------------------------------------+
-
- For speed I think a static array of masks to localize the search to
- (say four bits) followed by a sequential search might be good.
-